const app = getApp()

var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');


Page({
  setdata: app.setdata,
  goto: app.goto,

  data: {
    bannerList: [],
    nameCommunity:"请选择小区",
    noticeList:[],
    getPhoneNumberFlag:true
  },
  onLoad: function () {
    app.init(this);

  },
  onShow: function (e) {
    // 登录判断
    if (!wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return false;
    }

    // 是否选择小区判断
    if (!wx.getStorageSync('idCommunity')) {
      wx.navigateTo({
        url: '/pages/choice_community/choice_community',
      })
      app.toast("你还未选择小区，请先选择小区");
      return false;
    }

    // 是否选择小区判断
    if (wx.getStorageSync("longitude") == "") {
      wx.navigateTo({
        url: '/pages/choice_community/choice_community',
      })
      app.toast("你拒绝了地理位置授权，请重新授权地理位置");
      return false;
    }

    
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('nameCommunity')
    })
    //设置是否弹窗授权手机号
    this.setData({
      getPhoneNumberFlag: wx.getStorageSync("isBindPhoneFlag")
    });

    this.setData({
      nameCommunity: wx.getStorageSync('nameCommunity')
    });
   

    var bannerUrl = app.globalData.domain + "/clientPublic/getLunBo";
    var parm = {
      token: wx.getStorageSync("token"),
    }
    wxRequest.postRequest(bannerUrl, parm).then(res => {
      console.log("轮播图=>", res);
      this.setData({
        bannerList: res.data
      });
    }, error => {
      Common.showToast(error);
    });

    //获取公告列表
    var getIndexAnnouncementUrl = app.globalData.domain + "/clientPublic/getIndexAnnouncement";
    var parm = {
      token: wx.getStorageSync("token"),
    }
    wxRequest.postRequest(getIndexAnnouncementUrl, parm).then(res => {
      console.log("获取首页公告=>", res);
      this.setData({
        noticeList: res.data
      });
    }, error => {
      Common.showToast(error);
    });

    app.api.client_community_getAllBindRoom({
      token: wx.getStorageSync('token'),
      idCommunity: wx.getStorageSync('idCommunity'),
    }, (res) => {
      if (res.data.mrRoom != undefined){
        wx.setStorageSync('mrRoom', res.data.mrRoom);
      }
    })




    
  },

  //跳转外链
  linkUrl: function (e) {
    var url = e.currentTarget.dataset.url;
    console.log("e>>>", url)
    if (url) {
      wx.navigateTo({
        title: "",
        url: "/pages/out_web/out_web?url=" + url,
      })
    }

  },
  //暂无开放提示
  noOpening:function(){
    Common.showToast("功能暂未开放，敬请期待");
  },
  catchTouchMoveForNotice:function(event){
    return false
  },
  getPhoneNumber: function (e){
    var that = this;
    var url = app.globalData.domain + "/client/user/phone";
    console.log(e);
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      wx.login({
        success: res => {
          app.api.client_user_phone({
            token: wx.getStorageSync("token"),
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            code: res.code
          },(res)=>{
            wx.setStorageSync("isBindPhoneFlag", true)
            this.setData({
              getPhoneNumberFlag: wx.getStorageSync("isBindPhoneFlag")
            });
            
          })


        }
      })
    } else if (e.detail.errMsg == "getPhoneNumber:fail user deny"){
      Common.showToast("壹社圈邀请您授权手机号");
    } else {
      Common.showToast("请绑定手机号才授权");
    }

    
  },


  //跳转到家庭报修
  redirectToRepair: function () {
    if (wx.getStorageSync("idCommunity")) {
      wx.navigateTo({
        url: '/pages/family_repair_list/family_repair_list'
      })
    } else {
      Common.showToast("请选择所在小区");
    }
  },
  //跳转物业缴费
  redirectToBillHomePage: function () {
    if (wx.getStorageSync("idCommunity")) {
      wx.navigateTo({
        url: '/pages/bill_homepage/bill_homepage'
      })
    } else {
      Common.showToast("请选择所在小区");
    }
  },

  scanCode: function () {
    wx.scanCode({
      success(res) {
        console.log(res)
      }
    })
  },

  none: function () {
    app.toast("海南城市暂未开放此功能，敬请期待");
  },
  

})
